<style type="text/css">
	label.desc-lbl{
		text-indent:0.5rem;
	}
	label.switch{
		letter-spacing:0rem;
	}
	ul.set-con input.text{
		width:5.75rem;
		border:1px solid #E9E9E9;
		border-radius:4px;
		padding:0 0.25rem;
		color:#575757;
		height:1rem;
		font-size:0.4rem;
		vertical-align:middle;
	}
	ul.set-con input.text-eye{
		width:5rem;
		padding-right:1rem;
	}
	ul.set-con input.text:focus{
		border-color:#FFBE62;
	}
	i.tip{
		position:relative;
		float: right;
		top: -0.92rem;
		right: 0.6rem;
	}
	div.err-note,
	div.btn-con{
		clear:both;
	}
</style>
<script type="text/javascript">
(function(){
	<%
		local para = {
			wireless = {smart_connect = {}, wifi_2g_config = {}, wifi_5g_config = {}}
		}

		local data = tpl_get_data(para, true)
	-%>

	var wirelessInfo = <%=data%>;

	function init(result){
		var errNo = result[ERR_CODE];
		var ret;
		if (E_NONE == errNo){
			ret = result[K_MODULE];
		}else{
			console.log(result);
			return;
		}

		var smartConnect = ret.wireless.smart_connect;
		var wireless2G = ret.wireless.wifi_2g_config;
		var wireless5G = ret.wireless.wifi_5g_config;
		$("#Ssid2G").val(wireless2G.ssid);
		$("#Ssid5G").val(wireless5G.ssid);
		$("#Pwd2G").val(wireless2G.password);
		$("#Pwd5G").val(wireless5G.password);

		initSwitch("Two2One", smartConnect.enable, function(state, init){
			if (0 == state){
				$("#Wifi5G").show();
				$("#WifiName").html(label.wifi2G);
			}else{
				$("#Wifi5G").hide();
				$("#WifiName").html(label.wifiName);
				closeNote();
			}

			if (!init){
				$("#Ssid5G").val(getStrInMaxByte($("#Ssid2G").val(), 29) + "_5G");
			}
		});

		initSwitch("Switch2G", wireless2G.enable);
		initSwitch("Switch5G", wireless5G.enable);
	}

	init(wirelessInfo);

	$("#Save").click(function(){
		if (1 == $("#Two2One").attr("data-value")){
			$("#Ssid5G").val($("#Ssid2G").val());
			$("#Pwd5G").val($("#Pwd2G").val());
			$("#Switch5G").attr("data-value" , $("#Switch2G").attr("data-value"));
		}

		if ($("#Ssid2G").val() == $("#Ssid5G").val()
			&& $("#Pwd2G").val() != $("#Pwd5G").val()){
			showNote(errStr.sameWifiPwd);
			return false;
		}

		if (0 == $("#Switch2G").attr("data-value")
			&& 0 == $("#Switch5G").attr("data-value")){
			showConfirm(errStr.closeWifi, function(choose){
				if (choose){
					saveWifi();
				}
			});
		}else{
			if ((1 == $("#Switch2G").attr("data-value") && $("#Pwd2G").val() == "")
				|| (1 == $("#Switch5G").attr("data-value") && $("#Pwd5G").val() == "")){
				showConfirm(errStr.emptyWifiPwd, function(choose){
					if (choose){
						saveWifi();
					}
				});
			}else{
				saveWifi();
			}
		}
	});

	function saveWifi(){
		var para = {};
		var smart_connect = {};
		var wifi_2g_config = {};
		var wifi_5g_config = {};
		para.wireless = {};
		para.wireless.smart_connect = smart_connect;
		para.wireless.wifi_2g_config = wifi_2g_config;
		para.wireless.wifi_5g_config = wifi_5g_config;
		smart_connect.enable = $("#Two2One").attr("data-value");
		wifi_2g_config.enable = $("#Switch2G").attr("data-value");
		wifi_2g_config.ssid = $("#Ssid2G").val();
		wifi_2g_config.password = $("#Pwd2G").val();
		wifi_5g_config.enable = $("#Switch5G").attr("data-value");
		wifi_5g_config.ssid = $("#Ssid5G").val();
		wifi_5g_config.password = $("#Pwd5G").val();

		if (errHandle(checkSsid2G(wifi_2g_config.ssid), smart_connect.enable)){
			return;
		}

		if (errHandle(checkPwd2G(wifi_2g_config.password), smart_connect.enable)){
			return;
		}

		if (errHandle(checkSsid5G(wifi_5g_config.ssid), smart_connect.enable)){
			return;
		}

		if (errHandle(checkPwd5G(wifi_5g_config.password), smart_connect.enable)){
			return;
		}

		showLoading(label.saveWifi);
		apiSet(para, function(ret){
			var code = ret[ERR_CODE];
			if (E_NONE == code){
				var time = max(ret[K_MODULE].wireless.smart_connect.wait_time, ret[K_MODULE].wireless.wifi_2g_config.wait_time,
								ret[K_MODULE].wireless.wifi_5g_config.wait_time);
				$.setTimeout(function(){
					stateman.go("phone.wifiSet");
				}, time * 1000);
			}else{
				errHandle(code, smart_connect.enable);
				closeLoading();
			}
		});
	}

	function errHandle(code, smartCode){
		var noteStr = "";
		if (smartCode == 0){
			switch(code){
			case E_NONE:
				return false;
			case E_WL2GSSIDBLANK:
				noteStr = errStr.ssidEmpty2G;
				break;
			case E_WL2GSSIDLEN:
				noteStr = errStr.ssidLength2G;
				break;
			case E_WL2GSSIDILLEGAL:
				noteStr = errStr.ssidInclSpeChar2G;
				break;
			case E_WL2GSSIDPWLEN:
				noteStr = errStr.ssidPwdLength2G;
				break;
			case E_WL2GSSIDPWILLEGAL:
				noteStr = errStr.ssidPwdInclSpeChar2G;
				break;
			case E_WL2GSSIDCONFICTWITHGUEST:
				noteStr = errStr.ssid2GVisSame;
				break;
			case E_WL5GSSIDBLANK:
				noteStr = errStr.ssidEmpty5G;
				break;
			case E_WL5GSSIDLEN:
				noteStr = errStr.ssidLength5G;
				break;
			case E_WL5GSSIDILLEGAL:
				noteStr = errStr.ssidInclSpeChar5G;
				break;
			case E_WL5GSSIDPWLEN:
				noteStr = errStr.ssidPwdLength5G;
				break;
			case E_WL5GSSIDPWILLEGAL:
				noteStr = errStr.ssidPwdInclSpeChar5G;
				break;
			case E_WL5GSSIDCONFICTWITHGUEST:
				noteStr = errStr.ssid5GVisSame;
				break;
			default:
				noteStr = errStr.unknownErr + code;
				break;
			}
		}else{
			switch(code){
			case E_NONE:
				return false;
			case E_WL2GSSIDBLANK:
			case E_WL5GSSIDBLANK:
				noteStr = errStr.ssidEmpty;
				break;
			case E_WL2GSSIDLEN:
			case E_WL5GSSIDLEN:
				noteStr = errStr.ssidLength;
				break;
			case E_WL2GSSIDILLEGAL:
			case E_WL5GSSIDILLEGAL:
				noteStr = errStr.ssidInclSpeChar;
				break;
			case E_WL2GSSIDPWLEN:
			case E_WL5GSSIDPWLEN:
				noteStr = errStr.ssidPwdLength;
				break;
			case E_WL2GSSIDPWILLEGAL:
			case E_WL5GSSIDPWILLEGAL:
				noteStr = errStr.ssidPwdInclSpeChar;
				break;
			case E_WL2GSSIDCONFICTWITHGUEST:
			case E_WL5GSSIDCONFICTWITHGUEST:
				noteStr = errStr.ssidVisSame;
				break;
			default:
				noteStr = errStr.unknownErr + code;
				break;
			}
		}

		showNote(noteStr);

		return true;
	}

	function checkSsid2G(ssid){
		var result = checkSsid(ssid)

		if (result == E_SSID_BLANK){
			return E_WL2GSSIDBLANK;
		}

		if (result == E_SSID_LEN){
			return E_WL2GSSIDLEN;
		}

		return E_NONE;
	}

	function checkPwd2G(password){
		var result = checkWlanPwd(password)

		if ("" == password){
			return E_NONE;
		}

		if (result == E_WIFI_PWD_LEN){
			return E_WL2GSSIDPWLEN;
		}

		if (result == E_WIFI_PWD_ILLEGAL){
			return E_WL2GSSIDPWILLEGAL;
		}

		return E_NONE;
	}

	function checkSsid5G(ssid){
		var result = checkSsid(ssid)

		if (result == E_SSID_BLANK){
			return E_WL5GSSIDBLANK;
		}

		if (result == E_SSID_LEN){
			return E_WL5GSSIDLEN;
		}

		return E_NONE;
	}

	function checkPwd5G(password){
		var result = checkWlanPwd(password)

		if ("" == password){
			return E_NONE;
		}

		if (result == E_WIFI_PWD_LEN){
			return E_WL5GSSIDPWLEN;
		}

		if (result == E_WIFI_PWD_ILLEGAL){
			return E_WL5GSSIDPWILLEGAL;
		}

		return E_NONE;
	}
})();
</script>
<div class="head-con">
	<a href="javascript:logout();" class="head-btn-l">{%btn.logout%}</a>
	<h1 class="head-title">{%label.wifiSet%}</h1>
</div>
<div class="set-con below-head">
	<ul class="set-con hidden">
		<div class="switch-con">
			<label class="desc-title">{%label.twoInone%}</label>
			<span id="Two2One" class="switch">
				<i class="switch-circle"></i>
			</span>
		</div>
		<p class="desc-tetail">{%label.wifiRemark%}</p>
	</ul>
	<ul class="set-con-title">
		<label class="title-desc-lbl" id="WifiName">{%label.wifi2G%}</label>
	</ul>
	<ul class="set-con">
		<label class="desc-lbl switch">{%label.wifiSwitch%}</label>
		<span id="Switch2G" class="switch">
			<i class="switch-circle"></i>
		</span>
	</ul>
	<ul class="set-con">
		<label class="desc-lbl">{%label.ssidName%}</label>
		<input id="Ssid2G" class="text hover" maxLength="32" value="" />
	</ul>
	<ul class="set-con">
		<label class="desc-lbl">{%label.password%}</label>
		<input id="Pwd2G" class="text hover text-eye" maxLength="63" value="" />
		<i class="tip icon-eye" data-value="0"></i>
	</ul>
	<div id="Wifi5G">
		<ul class="set-con-title">
			<label class="title-desc-lbl">{%label.wifi5G%}</label>
		</ul>
		<ul class="set-con">
			<label class="desc-lbl switch">{%label.wifiSwitch%}</label>
			<span id="Switch5G" class="switch">
				<i class="switch-circle"></i>
			</span>
		</ul>
		<ul class="set-con">
			<label class="desc-lbl">{%label.ssidName%}</label>
			<input id="Ssid5G" class="text hover" maxLength="32" value="" />
		</ul>
		<ul class="set-con">
			<label class="desc-lbl">{%label.password%}</label>
			<input id="Pwd5G" class="text hover text-eye" maxLength="63" value="" />
			<i class="tip icon-eye" data-value="0"></i>
		</ul>
	</div>
	<div class="err-note hidden">
		<i class="err-icon icon_remind_line"></i>
		<p class="note-str"></p>
	</div>
	<div class="btn-con">
		<input id="Save" class="btn" type="button" value="{%btn.save%}" />
	</div>
</div>